package com.yirui.serialcomm.core.util;

import lombok.extern.slf4j.Slf4j;

/**
 * 〈功能简述〉
 *  平台配置参数
 *
 * @author Dujie
 * @create 2019/9/16
 * @since 1.0.0
 */
@Slf4j
public enum Platform {
    Linux,
    Windows,
    OS_X,
    Solaris,
    FreeBSD;

    public final static Platform CURRENT;
    private static final String DEFAULT_CHARSET = "UTF-8";
    static {
        CURRENT = detect();
    }

    private static final String OS_NAME = "os.name";
    private static final String LINUX = "Linux";
    private static final String WINDOWS = "Windows";
    private static final String MAC_OS_X = "Mac OS X";
    private static final String SUN_OS = "SunOS";
    private static final String FREE_BSD = "FreeBSD";
    private static final String USER_LANGUAGE = "user.language";
    private static final String SUN_JNU_ENCODING = "sun.jnu.encoding";
    private static final String FILE_ENCODING = "file.encoding";

    public static Platform detect() {
        String osName = System.getProperty(OS_NAME);
        if (osName.equals(LINUX))
            return Linux;
        if (osName.startsWith(WINDOWS, 0))
            return Windows;
        if (osName.equals(MAC_OS_X))
            return OS_X;
        if (osName.contains(SUN_OS))
            return Solaris;
        if (osName.equals(FREE_BSD))
            return FreeBSD;
        throw new IllegalArgumentException("Could not detect Platform name: os.name=" + osName);
    }

    public boolean isUnixLike() {
        return this!=Windows;
    }
    public static boolean isUnix() {
        return CURRENT.isUnixLike();
    }

    /**
     * 是否为zh(中文)语言
     * @return
     */
    public static boolean isZHLang(){
        String lang = System.getProperty(USER_LANGUAGE);
        return "zh".equals(lang);
    }
    private static String encoding;
    public static String charset(String defaultCharset) {
        if(encoding == null) {
            encoding = System.getProperty(SUN_JNU_ENCODING, System.getProperty(FILE_ENCODING, defaultCharset));
            if (log.isDebugEnabled()) {
                log.info("Current charset:" + encoding + " of the Platform");
            }
        }
        return encoding;
    }
    public static String charset(){
        return charset(DEFAULT_CHARSET);
    }

}
